<?php namespace Home\Controller;
use Common\Model\Addaddress;
use Common\Model\Coattribute;
use Common\Model\DetailInfo;
use Common\Model\Orderd;
use Common\Model\OrderList;
use Common\Model\User;
use Hdphp\Controller\Controller;

class PersonalController extends Controller{
    public function index(){
//        p($_SESSION);die;
        if($uid=Q('get.uid',0,'intval')){
            $uid=Q('get.uid',0,'intval');
        }else{
            $uid=$_SESSION['userinfo']['uid'];
        }
//        显示用户信息
        $userModel=new User();
        $userData=$userModel->where("uid={$uid}")->find();
//        显示用户收货地址
        $addaddressModel=new Addaddress();
        $addressData=$addaddressModel->where("shop_user_uid={$uid}")->get();
//        显示用户订单
        $orderModel=new Orderd();
        $orderData=$orderModel->join('orderlist ol','oid','=','ol.shop_orderd_oid')
            ->join('commodlist cl','ol.shop_commodlist_colid','=','cl.colid')
            ->join('commodity','ol.shop_commodity_coid','=','coid')
            ->where("shop_user_uid={$uid}")->get();

//        把订单ID相同的商品放在一起
        $sameoid=[];
        foreach($orderData as $u){
            $sameoid[$u['oid']][]=$u;
        }
        $coattModel=new Coattribute();
        foreach($sameoid as  $k=>$v){
            foreach($v as $kk=>$value){
                $sameoid[$k][$kk]['cogroupid']=explode(',',$value['cogroupid']);
                foreach($sameoid[$k][$kk]['cogroupid'] as $kkk=>$vv){
                    $sameoid[$k][$kk]['cogroupid'][$kkk]=$coattModel->where("coattid={$vv}")->pluck('coattvalue');
                }
                $sameoid[$k][$kk]['cogroupid']=implode(',',$sameoid[$k][$kk]['cogroupid']);
            }
        }

        $finalData=[];
        foreach($sameoid as $h=>$j){
            $finalData[$h]=[
                'ordernum'=>$orderModel->where("oid={$h}")->pluck('ordernum'),
                'ordertime'=>$orderModel->where("oid={$h}")->pluck('ordertime'),
                'orderpri'=>$orderModel->where("oid={$h}")->pluck('orderpri'),
                'orderstate'=>$orderModel->where("oid={$h}")->pluck('orderstate'),
                'value'=>$j,
            ];
        }
//        状态0代表待付款，状态1代表已取消，状态2代表已付款，状态3代表待收货
//        待付款数
        $num01=$orderModel->where("orderstate=0 AND shop_user_uid={$uid}")->count('*');
//        已取消
        $num02=$orderModel->where("orderstate=1 AND shop_user_uid={$uid}")->count('*');
//        已付款
        $num03=$orderModel->where("orderstate=2 AND shop_user_uid={$uid}")->count('*');
//        待收货
        $num04=$orderModel->where("orderstate=3 AND shop_user_uid={$uid}")->count('*');
//        p($num01);die;
        View::with('num01',$num01);
        View::with('num02',$num02);
        View::with('num03',$num03);
        View::with('num04',$num04);
        View::with('finalData',$finalData);
        View::with('userData',$userData);
        View::with('addressData',$addressData);
        View::make();
    }
//     用户中心修改密码方法
    public function changePassword(){
        if(IS_POST) {
//            p(Q('POST.'));die;
//        1判断新密码是否满足不少于六位的条件
            $newpassword = Q('post.newpassword');
            if (preg_match('/^\w{6,12}+$/', $newpassword) == 0) {
                View::error('请输入6到12位字母或数字下划线');
            }
            //        2判断新密码和确认密码是否相等
            $newpasswordagain = Q('post.newpasswordagain');
            if ($newpassword != $newpasswordagain) {
                View::error('新密码两次输入不一致');
            }
            //        3判断旧密码是否正确
            $oldpassword=Q('post.oldpassword','','md5');
            $uid=$_SESSION['userinfo']['uid'];
            $dataoldpassword=Db::table('user')->where("uid='{$uid}'")->pluck('userpassword');
            if($oldpassword!=$dataoldpassword){
                View::error('旧密码输入错误');
            }
            //        4进行修改密码
            Db::table('user')->where("uid={$uid}")->update(['userpassword'=>md5($newpassword)]);
            unset($_SESSION['userinfo']);
            View::success('密码修改成功',U('User/index'));
        }
        View::make();
    }
//    用户添加地址
    public function addAddress(){
        $addaddressModel=new Addaddress();
        if(IS_POST){
            if(!$addaddressModel->store()){
                View::error($addaddressModel->getError());
            }else{
                View::success('添加成功',U('index'));
            }
        }
        View::make();
    }

//    用户地址删除
    public function delAddress(){
        $rid=Q('get.rid',0,'intval');
        $addaddressModel=new Addaddress();
        $addaddressModel->where("rid={$rid}")->delete();
        View::success('删除成功',U('index'));
    }

//    用户地址编辑
//    显示待编辑的数据
    public function editData(){
        $rid=Q('post.r');
        $addaddressModel=new Addaddress();
        $oldaddData=$addaddressModel->where("rid={$rid}")->find();
        echo json_encode($oldaddData);exit;
    }
//    用户地址编辑
    public function editAddress(){
        $addaddressModel=new Addaddress();
        if(IS_POST){
            if(!$addaddressModel->edit()){
                View::error($addaddressModel->getError());
            }else{
                View::success('编辑成功',U('index'));
            }
        }
        View::make();
    }
//    用户地址选择
    public function chooseAddress(){
        $rid=Q('post.r');
        $uid=$_SESSION['userinfo']['uid'];
        $addaddressModel=new Addaddress();
        $addaddressModel->where("shop_user_uid={$uid}")->update(['restate'=>0]);
        $addaddressModel->where("rid={$rid} AND shop_user_uid={$uid}")->update(['restate'=>1]);
//        p($rid);
    }

//    用户详细信息表
    public function detailInfo(){
        $deinModel=new DetailInfo();
        if(IS_POST){
            if(!$deinModel->store()){
                View::error($deinModel->getError());
            }else{
                View::success('个人信息更改成功');
            }
        }
        View::make();
    }

//    用户信息异步提取
    public function getPersoninfo(){
        $uid=Q('post.u');
        $deinModel=new DetailInfo();
        $deinData=$deinModel->where("uid={$uid}")->find();
        $deinData['userbirthday']=explode('-',$deinData['userbirthday']);
        foreach($deinData['userbirthday'] as $k=>$u){
            $deinData[$k]=$u;
        }
        $deinData['year']=$deinData[0];
        $deinData['month']=$deinData[1];
        $deinData['day']=$deinData[2];
//        p($deinData);die;
        echo json_encode($deinData);exit;
    }

    //uploadify异步上传
    public function upload()
    {
        $file = Upload::path('Upload/Content/' . date('y/m'))->make();
        if (empty($file)) {
            // 相当于：echo json_encode(Upload::getError());exit;
            $this->ajax(Upload::getError());
        } else {
            /** $file内部就是以下这个数组
             * $file = array(
             * 0 => array(
             * 'path' => 'Upload/Content/15/8/123981239172.jpg',
             * 'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
             * 'image' => 1
             * ),
             * );**/
            //上传成功，把上传好的信息返给js 也就是uploadify
            $data = $file[0];
            // 相当于：echo json_encode($data);exit;
            $this->ajax($data);
        }
    }

    //    异步删除图片
    public function deleteimg(){
        $path=Q('post.path');
//        p($path);die;
        unlink($path);
    }
//    取消订单
    public function quxiao(){
        $orderModel=new Orderd();
        $oid=Q('get.oid');
        $orderModel->where("oid={$oid}")->update(['orderstate'=>1]);
        View::success('操作成功');
//        p($oid);die;
    }

//    支付订单
    public function zhifu(){
        $orderModel=new Orderd();
        $oid=Q('get.oid');
        $orderModel->where("oid={$oid}")->update(['orderstate'=>2]);
        View::success('操作成功');
    }

//    恢复订单
    public function huifu(){
        $orderModel=new Orderd();
        $oid=Q('get.oid');
        $orderModel->where("oid={$oid}")->update(['orderstate'=>0]);
        View::success('操作成功');
    }
//删除订单
    public function deleteorder(){
        $orderModel=new Orderd();
        $orderlistModel=new OrderList();
        $oid=Q('get.oid');
        $orderModel->join('orderlist ol','oid','=','ol.shop_orderd_oid')->where("oid={$oid}")->delete();
        $orderlistModel->where("shop_orderd_oid={$oid}")->delete();
        View::success('删除订单成功');
    }
}